﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriganleTests.Question;

namespace TriganleTests.Answer
{
    class Effie:ITriangle
    {
        public bool Triangle(MyPoint A, MyPoint B, MyPoint C)
        {
            //Get line function
            double k=0;
            double b=0;
            if (B.Y == A.Y)
                k = 0;
            else
                k = (B.X - A.X) / (B.Y - A.Y);
            b = A.Y - k * A.X;
            //Assert C is in line AB
            if (C.Y == k * C.X + b)
                return false;
            else
                return true;
        }
        public double GetLength(MyPoint A, MyPoint B)
        {
            double c = (B.X - A.X) * (B.X - A.X) + (B.Y - A.Y) * (B.Y - A.Y);
            return Math.Sqrt((B.X - A.X) * (B.X - A.X) + (B.Y - A.Y) * (B.Y - A.Y));
        }
        public double GetSquare(MyPoint A, MyPoint B, MyPoint C)
        {
            double square=0;
            if (Triangle(A, B, C))
            {
                double len1 = GetLength(A, B);
                double len2 = GetLength(B, C);
                double len3 = GetLength(A, C);
                double m = (len1 + len2 + len3) / 2;
                double h = m * (m - len1) * (m - len2) * (m - len3);
                square = Math.Sqrt(h); //海伦公式 
            }
            return square;
        }

        public int InTriangle(MyPoint A, MyPoint B, MyPoint C, MyPoint test)
        {

            if (!Triangle(A, B, C))
                return -1;
            else
            {
                

                if (Triangle(A, B, test) && Triangle(A, C, test) && Triangle(B, C, test))
                {
                    double a = GetSquare(A, B, test);
                    double b = GetSquare(B, C, test);
                    double c = GetSquare(A, C, test);
                    double d = GetSquare(A, B, C);

                    if (Math.Abs(a + b + c - d) < 0.000001)
                        return 1;
                    else
                        return 0;
                }
                else
                {
                    double a = GetSquare(A, B, test);
                    double b = GetSquare(B, C, test);
                    double c = GetSquare(A, C, test);
                    double d = GetSquare(A, B, C);

                    if (Math.Abs(a + b + c - d) < 0.000001)
                        return 1;
                    else
                        return 0;
                }  
            }

        }

    }
}
